package top.abilities.util.util;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * @Auhtor: ciro
 * @Description
 * @Date: 2017/12/11 15:12
 * @Modified By:
 */
public class NetworkUtil {

    private static Logger logger= LoggerFactory.getLogger(NetworkUtil.class);

    public static String getIpAdress(HttpServletRequest request){

        //获取请求主机ip地址，如果请求主机通过代理进来，真实ip地址
        String ip=request.getHeader("X-Forwarded-For");
        if(logger.isInfoEnabled()) logger.info("getIpAddress(HttpServletRequest) - X-Forwarded-For - String ip="+ip);
        if (ip==null||ip.length()==0||"unknown".equalsIgnoreCase(ip)){
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
                if (logger.isInfoEnabled()&&(!StringUtils.isEmpty(ip) &&!"unknown".equalsIgnoreCase(ip))) {
                    logger.info("getIpAddress(HttpServletRequest) - Proxy-Client-IP - String ip=" + ip);
                }
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
                if (logger.isInfoEnabled()&&(!StringUtils.isEmpty(ip) &&!"unknown".equalsIgnoreCase(ip))) {
                    logger.info("getIpAddress(HttpServletRequest) - WL-Proxy-Client-IP - String ip=" + ip);
                }
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
                if (logger.isInfoEnabled()&&(!StringUtils.isEmpty(ip) &&!"unknown".equalsIgnoreCase(ip))) {
                    logger.info("getIpAddress(HttpServletRequest) - HTTP_CLIENT_IP - String ip=" + ip);
                }
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
                if (logger.isInfoEnabled()&&(!StringUtils.isEmpty(ip) &&!"unknown".equalsIgnoreCase(ip))) {
                    logger.info("getIpAddress(HttpServletRequest) - HTTP_X_FORWARDED_FOR - String ip=" + ip);
                }
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
                if (logger.isInfoEnabled()&&(!StringUtils.isEmpty(ip) &&!"unknown".equalsIgnoreCase(ip))) {
                    logger.info("getIpAddress(HttpServletRequest) - getRemoteAddr - String ip=" + ip);
                }
            }
        }else if (ip.length()>15){
            String[] ips = ip.split(",");
            for (int index = 0; index < ips.length; index++) {
                String strIp = (String) ips[index];
                if (!("unknown".equalsIgnoreCase(strIp))) {
                    ip = strIp;
                    break;
                }
            }
        }
        return ip;
    }
}
